package Offer2017;

/**
 * 连续子数组的最大和
 * Created by 雷文康 on 2018/1/1.
 */
public class Test31 {
    public static void main(String[] args) {
//        int[] array = {1,-2,3,10,-4,7,2,-5};
        int[] array1 = {-5,-4,-3,-2,-1};
        System.out.println(findMaxOfSubArr(array1));
    }
    public static int findMaxOfSubArr(int[] array){
        if (array == null || array.length == 0){
            return -1;
        }
        int sum = 0;
        int result = Integer.MIN_VALUE;
        for (int i = 0; i < array.length; i++) {
            if (sum <= 0) {
                sum = array[i];
            } else {
                sum += array[i];
            }
            if (sum > result) {
                result = sum;
            }
        }
        return result;
    }
}
